// 22/02/2008
// Repartir sur les bases du multiservo (mecanisme d'interrutpion periodique)
// pur controler l'affichage des dotmatrix



#include<p18f4620.h>
#include <delays.h>
#include <timers.h>
#include <string.h>
#pragma config OSC=HS
#pragma config WDT=OFF
#pragma config LVP=OFF
#define true 1
#define false 0



typedef unsigned int boolean; 
unsigned int freq=100; //500; //5000; // for 20ms in CCP1
unsigned char nb,tic; 
unsigned char array[]={	255,9,9,9,255, // A
					   255,73,73,79,120, // B
					   255,65,65,65,65, // C
					   255,65,65,34,28, // D
					   255,73,73,73,73, //E
					   255,9,9,9,1, // F
					   255,65,73,73,121, // G
					   255,8,8,8,255, //H
					   0,0,125,0,0, //I
					   112,64,64,64,125, // J
					   255,8,24,36,66, // K
					   255,64,64,64,64, // L
					   255,2,4,2,255, // M
					   255,14,24,112,255, // N
					   255,65,65,65,255, // O
					   255,9,9,9,15, // P
					   15,9,9,9,255, // Q
					   255,9,25,41,79, // R
					   79,73,73,73,121, // S
					   1,1,255,1,1, // T
					   255,64,64,64,255, // U
					   15,56,64,56,15, // V
					   31,96,24,96,31, // W
					   99,20,8,20,99, // X
					   1,6,124,6,1, // Y
					   97,81,73,69,67, // Z
					   4,2,255,0,0, // 1
					   99,115,89,77,67 //2
					   };
unsigned char *ptr; 
	
void InterruptHandlerHigh (void);




void main(void){

	tic=0;
	// RB0-RB4 : colonne(+ : active)
	// RD0-RD6 : ligne (-b : active)
	//TODO
	TRISB=0; 
	TRISD=0; 
	// Compare mode will fire interrupt after 20ms
	CCPR1H=freq/256; 
	CCPR1L=freq%256; 
	// Allow interrupts
	INTCONbits.GIE=1; 
	INTCONbits.PEIE=1; 
	// Interrupt priority feature enabled
	RCONbits.IPEN=1; 
	// Compare mode generating interrupt
	CCP1CON=0b00001011; 
	// Compare interrupt
	PIE1bits.CCP1IE=1;
	IPR1bits.CCP1IP=1; 
	// Configure Timer 1
	T1CONbits.RD16=1; // 16 bits
	// enable timer 1; 
	T1CONbits.TMR1ON=1; 
	PORTB=1;
	ptr=array; 
	nb=0; 
	while(true){
		
			Delay10TCYx(255);
			ptr=ptr+5;
			nb++;  
			if(nb==28) {
				ptr=array;
				nb=0;}
				
		}
	}
	
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void
InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}
//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void
InterruptHandlerHigh ()
{
unsigned char i; 
if(PIR1bits.CCP1IF){
	unsigned char masque=1,tempo; 
	CCPR1H=freq/256; 
	CCPR1L=freq%256;
	for(i=0;i<7;i++){
		tempo=~(ptr[tic]&masque); 
		LATD=tempo; 
	Delay100TCYx(5);
	masque=masque<<1;
	}
	tempo=LATB<<1; 
	LATB=tempo; 
	tic++; 
	if(tic==5){
		tic=0; 
		LATB=1; 
		}
	PIR1bits.CCP1IF=0;
	}
}

